package advertising

import (
	"errors"
	"gitee.com/alex_li/amazon-rpt/internal/rest"
)

type NegativeReq struct {
	CampaignId  string `json:"campaignId,omitempty"`
	MatchType   string `json:"matchType,omitempty"`
	State       string `json:"state,omitempty"`
	AdGroupId   string `json:"adGroupId,omitempty"`
	KeywordText string `json:"keywordText,omitempty"`
}

type ErrorResp struct {
	Error []any `json:"error,omitempty"`
}

type SuccessMeta struct {
	CampaignNegativeKeywordId string `json:"campaignNegativeKeywordId,omitempty"`
	NegativeKeywordId         string `json:"negativeKeywordId,omitempty"`
	Index                     int    `json:"index,omitempty"`
}

type SuccessMetaWrapper struct {
	Success []SuccessMeta `json:"success,omitempty"`
}

type NegativeResp struct {
	ErrorResp
	SuccessMetaWrapper
}

const (
	negativePhrase = "NEGATIVE_PHRASE"
	negativeExact  = "NEGATIVE_EXACT"
)

var keyword = map[string]string{
	"campaign": "/sp/campaignNegativeKeywords",
	"adGroup":  "/sp/negativeKeywords",
}

var targeting = map[string]string{
	"campaign": "/sp/campaignNegativeTargets",
	"adGroup":  "/sp/negativeTargets",
}

func (c *Client) NegativeKeyword(category string, req []NegativeReq) (err error) {

	resp := make(map[string]NegativeResp)
	r, err := c.request()
	if err != nil {
		return
	}
	action, ok := keyword[category]
	if !ok {
		err = errors.New("category not found")
		return
	}
	err = r.Path(action).Body(req).Expect(207).Response(&resp).Json(rest.POST)
	if err != nil {
		return
	}
	return
}

func (c *Client) NegativeTargeting(category string, req []NegativeReq) (err error) {
	resp := make(map[string]NegativeResp)
	r, err := c.request()
	if err != nil {
		return
	}
	action, ok := targeting[category]
	if !ok {
		err = errors.New("category not found")
		return
	}
	err = r.Path(action).Body(req).Expect(207).Response(&resp).Json(rest.POST)
	if err != nil {
		return
	}
	return
}
